package javacommon.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

public class ScriptFilter extends OncePerRequestFilter implements Filter {
	public void destroy() {
		// TODO 自动生成方法存根

	}
//添加路径识别startthread
	@Override
	protected void doFilterInternal(HttpServletRequest request,HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String uri = request.getRequestURI();
		System.out.println(uri);
		checkScript(request);
	
		filterChain.doFilter(request, response);
	}
	/**屏蔽<>脚本
	 * */
	public void checkScript(ServletRequest request){
		Enumeration<String> enu = request.getParameterNames();
		Map<String,String[]> pMap = request.getParameterMap();
        while (enu.hasMoreElements()) {
            Object paraName = enu.nextElement();
            String[] value =(String[]) pMap.get(paraName);
            if(value.length ==0) {
                continue;
            }
            for(int i=0;i<value.length;i++){
            	//如需替换别的字符，可自行添加
            	value[i] = value[i].replaceAll("<","&lt;");
            	value[i] = value[i].replaceAll(">","&gt;");
            	value[i] = value[i].replaceAll("'","&acute;");
            }
        }
	}
	public static void main(String[] args) throws UnsupportedEncodingException{
		System.out.println("asdf".indexOf(""));
		System.out.println (URLDecoder.decode("%2F","GBK"));
		System.out.println (URLDecoder.decode("%2F","UTF-8"));
	}
}
